<?php
/**
 * Created by PhpStorm.
 * User: songs
 * Date: 2020/7/13
 * Time: 14:50
 */

namespace app\components\Api;

use app\components\Fun;
use app\components\UploadFile;
use app\models\user\gc\ChanliangchannengGcModel;
use app\models\user\gc\User_basechanpin_gcModel;
use app\models\user\gc\Users_zhengjianshangchuan_gcModel;
use app\models\user\gc\UsersBaseGcModel;
use app\models\user\gc\UsersBaseinfoGc;
use app\models\user\gr\AvatarModel;
use app\models\user\gr\UserOtherinfoGr;
use app\models\user\gr\Users_zhengjianshangchuan_GrModel;
use app\models\user\gr\UsersBaseGrModel;
use app\models\user\gr\UsersBaseinfoGr;
use app\models\user\gc\UserShebeiGcModel;
use app\models\user\gc\UsersHuanjingGc;
use app\models\user\gc\UserZhiliangyaoqiuGc;
use app\models\user\gs\User_basechanpin_gsModel;
use app\models\user\gs\Users_zhengjianshangchuan_gsModel;
use app\models\user\gs\UsersBaseGsModel;
use app\models\user\gs\UsersBaseinfoGs;
use  app\components\OA;
use app\models\user\gs\UsersHuanjingGs;
use app\models\user\IdentificationList;
use Yii;
use app\components\API;
use app\models\User;
use function Composer\Autoload\includeFile;

class UserSystemInfo
{

    /**
     * @apiGroup 用户
     * @api {post} /api/app/call/upUserPicture 修改用户头像  test
     * @apiParam {string} base64 头像的图片base64编码
     * @apiParam {string} identification_password 用于鉴别身份的鉴别密码,通过getIdentification获取
     * @apiSuccessExample 返回
     *
     * {"code":0,"data":"http:\/\/test.fuzhuangchangongxiao.cn\/public\/Uploads\/image\/20200807\/20200807110128_19240big.jpg"}
     * @apiUse ERROR
     */
    public function upUserPicture($post)
    {
        //exit("aaaa");
        header('Access-Control-Allow-Origin:*');

        $identification_password = $post['identification_password'] ?? '';
        $uid = IdentificationList::getUid($identification_password);
        //  exit("aaa");
        if (!$uid) {
            API::echo_json(API::guest, "没有登录,后者缺少用户身份鉴别标识");
        }

        $data = UploadFile::forBaseCodeUpfile($post['base64']);
        // print_r($data);exit;

        $count = \app\models\user\AvatarModel::upUserAvatar($data['file'], $data['img'], $uid);
        User::updateAll(['avatar' => $data['img']], ['id' => $uid]);
        if ($count !== false) {
            API::echo_json(0,
                Fun::$HOST . '/api/app/avatar?uid=' . $uid . '&time=' . time()
            );
            // API::echo_json(0,Fun::$HOST. $data['img']);
        } else {
            API::echo_json(1, '系统错误');
        }

    }


    /**
     * @apiGroup 用户
     * @api {post} /api/app/call/SetDataViewInput 快捷设置代码
     * @apiParam {string} field 有模块名和字段名组成   (User:username 修改用户昵称)  (User:mail 修改用户邮箱)
     * @apiParam {string} value 设置的值
     * @apiSuccessExample 返回
     *
     * {"code":0,"data":"ok"}
     * @apiUse ERROR
     */
    public function SetDataViewInput($post)
    {
        $fieldList = [
            'User:username' => User::class,
            'User:mail' => User::class,
            ///   'ChanliangchannengGcModel:zhengjian'=>ChanliangchannengGcModel::class,
        ];
        $jianzhi = 'uid';

        $field = $post['field'];
        $value = $post['value'];
        $list = explode(':', $field);
        $model = $fieldList[$field] ?? false;
        if (!$model) {
            API::echo_json(1, '参数错误,或者未配置接口');
        }
        if ($model === User::class) {
            $jianzhi = 'id';
        }
        $one = $model::updateAll([
            $list[1] => $value
        ], [
            $jianzhi => Yii::$app->user->id
        ]);
        if ($one === false) {
            API::echo_json(1, '数据查询失败,未能修改数据');
        } else {
            if ($field === 'User:username') {
                $red = JMessageApi::upNameNick(Yii::$app->user->id, $value);
            }
            API::echo_json(0, 'ok');
        }
    }

    /**
     * @apiGroup 用户
     * @api {post} /api/app/call/getUserAvatar 批量获取用户头像,用户极光显示头像
     * @apiParam {string} jiguang_id 用,分割的用户极光id
     * @apiSuccessExample 返回
     *
     * {"code":0,"data":{"admin":"http:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/mECo2RefIzQJBaZoibKdviccZURsYMHY6ACEIIp73Is0lLpnic8jlVMl5Sia09icnTCf5ekUr8zrwqPG4MfJlQOxfyQ\/132","mc_1":"http:\/\/www.fuzhuangchangongxiao.cn\/h5\/images\/toux01.png","mc_c36d17de6c38d36add269dec45191ec8":"http:\/\/www.fuzhuangchangongxiao.cn\/h5\/images\/toux01.png"}}
     * @apiUse ERROR
     */
    public function getUserAvatar($post)
    {
        $jiguang_id = $post['jiguang_id'];
        if (empty($post['jiguang_id'])) {
            API::echo_json(1, "参数错误");
        }
        if (strlen($post['jiguang_id']) > 3600) {
            API::echo_json(1, "jiguang_id参数太长了,不能超过3600字节");
        }
        $list = explode(',', $jiguang_id);
        $userlistmodel = User::find()->where([
            'in', 'jiguang_id', $list
        ])->select('jiguang_id,avatar')->all();
        $outlist = [];
        foreach ($userlistmodel as $vo) {
            $outlist[$vo['jiguang_id']] = $vo['avatar'];
        }
        API::echo_json(0, $outlist);
    }

    /**
     * @apiGroup 用户
     * @api {post} /api/app/call/getIdentification 获取用户的鉴别密码,用于跨域请求时附加,然后判断用户的身份
     * @apiSuccess {string} data 用户的鉴别密码,有效期为60X60X24X7秒
     * @apiSuccessExample 返回
     *
     * {"code":0,"data":"1755c6778422b047dbc13391dabe6c7c"}
     * @apiUse ERROR
     */
    public function getIdentification($post)
    {
        $identification = IdentificationList::createIdentification(Yii::$app->user->id);
        API::echo_json(0, $identification);
    }

    /**
     * @apiGroup 用户
     * @api {post} /api/app/call/getXieyi 获取协议 协议文件在./php/data/xieyi.txt
     * @apiSuccess {string} data 协议内容
     * @apiSuccessExample 返回
     *
     * {"code":0,"data":"1755c6778422b047dbc13391dabe6c7c"}
     * @apiUse ERROR
     */
    public function getXieyi($post)
    {
       // $type = $post['type'];
     /*   if ($type != 101 && $type != 102 && $type != 103) {
            API::echo_json(1, "参数错误");
        }*/
        $file_path = "../php/data/xieyi101.txt";
        if (!file_exists($file_path)) {
            API::echo_json(1, "协议不存在");
        }
        $text = file_get_contents($file_path);
        API::echo_json(0, $text);
    }


}